<template>
  <page-header-wrapper>
    <page-tab-list>
      <SearchBox @search="search"></SearchBox>
    </page-tab-list>

    <a-card :bordered="false">
      <div class="table-operator" style="display: flex; justify-content: space-between">
        <a-radio-group :defaultValue="-1" v-model="queryParam.status" @change="getOrderListData(true)">
          <a-radio-button v-for="(item, index) in options" :key="index" :value="item.value">
            {{ item.title }}
          </a-radio-button>
        </a-radio-group>
      </div>

      <OrderListTable ref="table" :orderList="pagaData" @triggerAction="triggerAction"></OrderListTable>

      <div style="float: right">
        <Pagination v-model="pageParams.page" :total="totalCount" @change="getOrderListData()" />
      </div>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import SearchBox from './components/SearchBox'
import { Pagination } from 'ant-design-vue'
import { getInstallLog, setInstallStatus } from '@/api/daili'
import OrderListTable from './components/OrderListTable.vue'

export default {
  name: 'OrderList',
  components: {
    SearchBox,
    Pagination,
    OrderListTable,
  },
  data() {
    return {
      data: [],
      columns: [],

      options: [
        {
          title: '全部',
          value: -1,
        },
        {
          title: '待配送',
          value: 0,
        },
        {
          title: '已配送',
          value: 1,
        },
        {
          title: '已完成',
          value: 2,
        },
      ],

      queryParam: {},
      totalCount: 0,
      pageParams: {
        page: 1,
        limit: 10,
      },
      pagaData: [],
    }
  },

  created() {
    this.getOrderListData()
  },

  methods: {
    // 点击搜索按钮
    search(e) {
      this.queryParam = e
      this.getOrderListData(true)
    },

    getOrderListData(clear = false) {
      if (clear) {
        this.pageParams.page = 1
      }

      let params = {
        ...this.queryParam,
        ...this.pageParams,
      }
      const hide = this.$message.loading('正在加载...')

      getInstallLog(params)
        .then((res) => {
          hide()

          if (res.code === 0) {
            this.pagaData = res.data.data
            this.totalCount = res.data.totalCount
          } else {
            that.$message.error(res.msg)
          }
        })
        .catch(() => {
          hide()
        })
    },

    // 点击按钮触发
    triggerAction(data) {
      let { type, order } = data
      switch (type) {
        case 'detail': //详情
          this.$router.push({
            path: '/saas/supplierPurchaseGoods/orderStatisticsDetails',
            query: { id: order.order_no, order_type: order.order_type },
          })
          break
        case 'deliver': //发货
          this.$confirm({
            title: '提示',
            content: '是否确认配送？',
            onOk: async () => {
              let res = await setInstallStatus({
                id: order.id,
              })
              if (!res.code) {
                this.getOrderListData()
                this.$message.success(res.msg)
                return
              }

              this.$message.error(res.msg)
            },
            onCancel() {},
          })
          break
      }
    },
  },
}
</script>
<style lang="less" scoped></style>
